home *** CD-ROM | disk | FTP | other *** search
/ The ZX Files 3 / ZX Files 3 (Disk 3 of 3).adf / MANUALV3 / ch8pt3 < prev    next >
Text File  |  1978-03-10  |  3KB  |  101 lines

  1. Part 3
  2. Decisions
  3.  
  4. Subjects covered...
  5.  
  6.     CLS, IF, STOP
  7.     =, <, >, <=, >=, <>
  8.  
  9. All the programs we have seen so far have been pretty predictable -
  10. they went straight through the instructions, and then went back to
  11. the beginning again. This is not very useful, as in practice, we would
  12. want the +3 to make decisions and act accordingly. The instruction to
  13. do this in BASIC takes the form...
  14.  
  15.     IF something is true (or not true) THEN do something
  16.  
  17. Let's look at an example of this. Use NEW to clear the previous
  18. program from the +3, select +3 BASIC, then type in and run this
  19. program. (This is clearly meant for two people to play!)...
  20.  
  21.     10 REM Guess the number
  22.     20 INPUT "Enter a secret number",a: CLS
  23.     30 INPUT "Guess the number",b
  24.     40 IF b=a THEN PRINT "That is correct": STOP
  25.     50 IF b<a THEN PRINT "That is too small, try again"
  26.     60 IF b>a THEN PRINT "That is too big, try again"
  27.     70 GO TO 30
  28.  
  29. Note that the CLS command (at the end of line 20) means 'clear the
  30. screen'. We have used it in this program to stop the other person
  31. seeing the secret number after it is entered.
  32.  
  33. You can see that the IF statement takes the form...
  34.  
  35.     IF condition THEN xxx
  36.  
  37. ...where 'xxx' stands for a command (or a sequence of commands
  38. separated by colons). The condition is something that is going to be
  39. worked out as either true or false - if it comes out as true then the
  40. statements in the reset of the line (after THEN) are executed;
  41. otherwise they are skipped over, and the program executes the next
  42. instruction.
  43.  
  44. The simplest conditions compare two numbers or two strings; they can
  45. test whether two numbers are equal or whether one is bigger than the
  46. other. They can also test whether two strings are equal, or whether
  47. one comes before the other in alphanumerical order. They use the
  48. symbols '=', '<', '>', '<=', '>=' and '<>' (these are known as
  49. relational operators).
  50.  
  51.     =    means is equal to.
  52.     <     means is less than.
  53.     >    means is greater than.
  54.     <=    means is less than or equal to.
  55.     >=    means is greater than or equal to.
  56.     <>    means is not equal to.
  57.  
  58. (If you keep getting mixed up about the meanings of '<' and '>', it
  59. may help you to remember that the thin end of the symbol points to the
  60. number which is supposed to be smaller.)
  61.  
  62. In the program we have just typed in, line 40 compares 'a' and 'b'. If
  63. they are equal, then the program is halted by the STOP command. The
  64. report at the bottom of the screen...
  65.  
  66.     9 STOP statement, 40:3
  67.  
  68. ...shows that the 3rd statement (i.e. the STOP command) in line 40
  69. caused the program to halt.
  70.  
  71. Line 50 determines whether 'b' is less than 'a', and line 60 whether
  72. 'b' is greater than 'a'. If one of these conditions is true then the
  73. appropriate comment is printer, and the program works its way down to
  74. line 70 which jumps back to line 30 and starts all over again.
  75.  
  76. Finally, note that in some versions of BASIC (not +3 BASIC) the IF
  77. statement can have the form...
  78.  
  79.     IF condition THEN line number
  80.  
  81. This means the same as...
  82.  
  83.     IF condition THEN GO TO line number
  84.  
  85. ...in +3 BASIC.
  86.  
  87.  
  88. Exercise...
  89.  
  90. 1. Try this program...
  91.  
  92.     10 LET a=1
  93.     20 LET b=1
  94.     30 IF a>b THEN PRINT a;" is higher"
  95.     40 IF a<b THEN PRINT b;" is higher"
  96.  
  97. [Yes, this is how the program actually reads in the manual. Of course,
  98. the contents of the PRINT statements don't really matter much.]
  99.  
  100. Before you run it, try to work out what will be printed on the screen.
  101.